package cn.learn.validator;

import cn.learn.SpringComponentApp;
import com.alibaba.fastjson.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/**
 * 测试验证密码，用这个测试不出来，只能启动鲜蘑菇，打开swagger来测试
 *
 * @author huangyezhan
 * @version 1.0
 * @date 2020年08月01日15:49
 */
//@RunWith(SpringRunner.class)   //在spring环境中运行  在使用所有注释前必须使用
//@RunWith（SpringJUnit4ClassRunner.class）//让测试运行于Spring测试环境
//@AutoConfigureMockMvc //是用于自动配置MockMvc  这个有问题,搞不掂
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {SpringComponentApp.class})
@WebAppConfiguration
public class ValidatorPassword {

    private MockMvc mockMvc;

    @Autowired
    private WebApplicationContext wac;

    @Before
    public void setup() {
        mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
    }

    @Test
    public void test1() throws Exception {
        MockHttpServletRequestBuilder builder = post("/valid/user2").contentType(MediaType.APPLICATION_JSON);
        MvcResult                     result  = mockMvc.perform(builder).andReturn();

        System.out.println(result.getResponse().getStatus());
        System.out.println(result.getResponse().getContentAsString());
    }

    @Test
    public void test2() throws Exception {
        MvcResult result = mockMvc.perform(post("/valid/user3")
                        .characterEncoding("UTF-8")
                        .param("accountName", "张三")
                        .param("accountType", "0")
                        .param("email", "string")
                        .param("gender", "0")
                        .param("id", "0")
                        .param("idNumber", "string")
                        .param("mobile", "1346566534")
                        .param("password", "Zw125")
                        .param("realName", "string")
                        .param("userId", "0")
        )
                //返回的状态码
                .andExpect(status().isOk())
                //打印详细的请求会响应信息
                .andDo(print())
                .andReturn();


        System.out.println("返回的状态码：" + result.getResponse().getStatus());
        System.out.println(result.getResponse().getContentAsString());

    }


    @Test
    public void test3() throws Exception {
        User user = new User();
        user.setAccountName("张三");
        user.setPassword("As009s");
        user.setAccountType((byte) 1);
        user.setGender((byte) 2);

        String jsonString = JSONObject.toJSONString(user);

        //用json方式传参
        MvcResult result = mockMvc.perform(post("/valid/user4")
                .characterEncoding("UTF-8")
                .contentType(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON)
                .content(jsonString)
        )
                .andExpect(status().isOk())
                .andDo(print())
                .andReturn();

        System.out.println("返回的状态码：" + result.getResponse().getStatus());
        System.out.println(result.getResponse().getContentAsString());

    }


}
